package com.aube.tinker;

import android.content.Context;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.FileRequest;
import com.huyn.baseframework.net.BFJSONRequest;
import com.huyn.baseframework.net.HttpRequestLoader;
import com.huyn.baseframework.utils.FileMD5Verify;

import java.util.HashMap;

/**
 * Created by huyaonan on 16/10/20.
 */
public class PatchDownloader {

    private static final String TAG = "PatchDownloader";

    private static PatchDownloader mInstance = null;
    private Context mContext;

    private PatchDownloader(Context context) {
        mContext = context;
    }

    public static PatchDownloader getInstance(Context context){
        if(mInstance == null){
            mInstance = new PatchDownloader(context.getApplicationContext());
        }
        return mInstance;
    }

    public void startPluginLoader(String url, String fileName, Response.Listener<String> mRequestListener) {
        FileRequest request = new FileRequest(url, mRequestListener, null);

        String pkgName = mContext.getPackageName().replace(".", "_");
        request.setFileTitle(pkgName + "_" + toApk(fileName));
        HttpRequestLoader.getInstance(mContext).startHttpLoader(request);
    }

    private String toApk(String name) {
        if(name.contains(".apk"))
            return name;
        if(name.contains(".")) {
            return name.substring(0, name.lastIndexOf(".")) + ".apk";
        } else {
            return name + ".apk";
        }
    }

    public void release() {
        mInstance = null;
    }

    public void getPatchInfo(String patchVersion, final IPatchLoadListener listener) {
        HashMap<String, String> params = new HashMap<>();
        params.put("method", "com.aube.video.theme");
        params.put("version", patchVersion);
        BFJSONRequest request = new BFJSONRequest(PatchModel.class, params, new Response.Listener<PatchModel>() {
            @Override
            public void onResponse(PatchModel response) {
                //parse string
                if(response != null && response.success()) {
                    downloadPlugin(response.data, listener);
                }
            }

            @Override
            public void onErrorResponse(VolleyError error) {

            }

            @Override
            public void onStart() {

            }
        });
        HttpRequestLoader.getInstance(mContext).startHttpLoader(request);
    }

    private void downloadPlugin(final Patch item, final IPatchLoadListener listener) {
        startPluginLoader(item.download, item.getFileName(), new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                boolean verifyResult = FileMD5Verify.md5sum(response, item.md5);
                if (verifyResult && listener != null)
                    listener.onSuccess(response);
            }

            @Override
            public void onErrorResponse(VolleyError error) {
            }

            @Override
            public void onStart() {

            }
        });
    }

    public interface IPatchLoadListener {
        public void onSuccess(String path);
    }

}
